//modified code originally from javaworld website -sujal
public class semaphore
{
    private int counter;

    public semaphore(int counter) 
    {
        this.counter=counter;
    }
    
    //Increments internal counter, possibly awakening a thread wait()ing in acquire().
    public synchronized void release() 
    {
        if (counter == 0) 
        {
            this.notify();
        }
        counter++;
    }

    // Decrements internal counter, blocking if the counter is already zero.
    //@exception InterruptedException passed from this.wait().
    public synchronized void acquire()
    {
        while (counter == 0) 
        {
            try
            {
                this.wait();
            }
            catch (InterruptedException ex)
            {

            }
        }
        counter--;
    }
}